package cn.edu.wynu;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@Aspect
public class AspectCalculator {

    @Pointcut("execution(public void cn.edu.wynu.Calculator.*(..))")
    void pointCut() {

    }

//    @Before("pointCut()")
    void beforeDeal(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        String methodName = signature.getName();
        int i = 1;
        System.out.print("method:" + methodName + ", ");
        for (Object arg : args) {
            System.out.print("arg" + i + "=" + arg + ", ");
            i++;
        }
    }

//    @AfterReturning("pointCut()")
    void afterDeal(JoinPoint joinPoint) {
        System.out.println("afterDeal");
    }

    @Around("pointCut()")
    public void dealAround(ProceedingJoinPoint pjb) {
        String methodName = pjb.getSignature().getName();
        long start = System.currentTimeMillis();
        Object[] args = pjb.getArgs();
        System.out.print("函数名：" + methodName + ",");

        for(Object arg: args) {
            System.out.print("arg:" + arg + ",");
        }
        try {
            pjb.proceed();
        } catch (Throwable e) {
            System.out.println("发现异常");;
        }
        long end = System.currentTimeMillis();
        System.out.println("函数执行时间:" + (end - start));
    }
}
